using System;
using System.Collections.Generic;
using System.Text;
using Lizk.SimpleHUD;
using SharpMedia.Pages;

namespace SharpMedia.Controls
{
    public class HUDTimeBar : HUDContainerSimple
    {
        HUDLabel labelPosition;
        HUDContainerSimple seekBar;
        TimedTrigger<TimeSpan> timer = new TimedTrigger<TimeSpan>(TimeSpan.Zero, new TimeSpan(0,0,0,0,650));

        public TimeSpan SeekTimeout
        {
            get { return timer.Time; }
            set { 
                timer.Time = value; 
            }
        }

        public TimeSpan SeekAmount
        {
            get { return timer.StateVar; }
            set {
                if (Position == TimeSpan.MinValue || Duration == TimeSpan.MinValue)
                    return;
                
                if (Position + value > Duration)
                    timer.StateVar = Duration - Position;
                else if (Position + value < TimeSpan.Zero)
                    timer.StateVar = Position.Negate();
                else
                    timer.StateVar = value;
                
                UpdateLabels();
            }
        }

        public bool IsSeeking { get { return timer.Triggered; } }

        private TimeSpan duration = TimeSpan.MinValue;

        public TimeSpan Duration
        {
            get { return duration; }
            set { 
                duration = value;
                UpdateLabels();
            }
        }

        private TimeSpan position = TimeSpan.MinValue;

        public TimeSpan Position
        {
            get { return position; }
            set { 
                position = value;
                UpdateLabels();
            }
        }

        private void UpdateLabels()
        {
            if (Position.Ticks <= 0 || Duration.Ticks <= 0)
            {
                seekBar.Size = new SizeS(0, 23);
            }
            else
            {
                seekBar.Size = new SizeS((int)((float)(Position + timer.StateVar).Ticks / (float)Duration.Ticks * (float)Bounds.Width - 4), 23);

                labelPosition.Text = FormatTime(Position + timer.StateVar);
                labelPosition.Size = labelPosition.Text.Length > 5 ? new SizeS(93, labelPosition.Size.Height) : new SizeS(65, labelPosition.Size.Height);
                labelPosition.Location = seekBar.Bounds.Width < labelPosition.Bounds.Width + 2 ? new PointS(3, 1) : new PointS(seekBar.Bounds.Right - labelPosition.Bounds.Width - 2, 1);
            }
            //labelDuration.Text = FormatTime(video.Duration);
        }


        private static string FormatTime(TimeSpan ts)
        {
            string deString = string.Format("{0}:{1}", ts.Minutes.ToString().PadLeft(2, '0'), ts.Seconds.ToString().PadLeft(2, '0')); ;
            if (ts.Hours > 0)
            {
                deString = ts.Hours.ToString() + ":" + deString;
            }
            return deString;
        }

        public event EventHandler SeekBegun, SeekEnded;

        public HUDTimeBar()
        {
            this.Size = new SizeS(200,27);

            timer.FirstTrigger += new EventHandler(timer_FirstTrigger);
            timer.TimeOut += new EventHandler(timer_TimeOut);
            
            seekBar = new HUDContainerSimple();
            seekBar.Anchor = Anchor.Left | Anchor.Bottom;
            seekBar.Bounds = new RectangleS(2, 2, 2, 23);
            Add(seekBar);

            HUDImage subSeekbar = new HUDImage("pb_seekbar.png");
            subSeekbar.ConstrainProportions = false;
            subSeekbar.Anchor = Anchor.Left | Anchor.Right | Anchor.Bottom;
            subSeekbar.Size = seekBar.Size;
            seekBar.Add(subSeekbar);

            //seekbar marker
            HUDImage seekBarTop = new HUDImage("white.png");
            seekBarTop.Anchor = Anchor.Top | Anchor.Right;
            seekBarTop.Alpha = 153;
            seekBarTop.ConstrainProportions = false;
            seekBarTop.Bounds = new RectangleS(seekBar.Size.Width - 1, 0, 1, subSeekbar.Bounds.Height);
            seekBar.Add(seekBarTop);

            // seekbar top
            seekBarTop = new HUDImage("white.png");
            seekBarTop.Anchor = Anchor.All ^ Anchor.Top;
            seekBarTop.ConstrainProportions = false;
            seekBarTop.Bounds = new RectangleS(0, 0, Bounds.Width, 1);
            Add(seekBarTop);

            // bottom bar
            seekBarTop = new HUDImage("white.png");
            seekBarTop.Anchor = Anchor.All ^ Anchor.Top;
            seekBarTop.ConstrainProportions = false;
            seekBarTop.Bounds = new RectangleS(0, Bounds.Height - 1, Bounds.Width, 1);
            Add(seekBarTop);

            // left bar
            seekBarTop = new HUDImage("white.png");
            seekBarTop.Anchor = Anchor.Left ^ Anchor.Top;
            seekBarTop.ConstrainProportions = false;
            seekBarTop.Bounds = new RectangleS(0, 1, 1, Bounds.Height - 2);
            Add(seekBarTop);

            // right bar
            seekBarTop = new HUDImage("white.png");
            seekBarTop.Anchor = Anchor.Right | Anchor.Top;
            seekBarTop.ConstrainProportions = false;
            seekBarTop.Bounds = new RectangleS(Bounds.Width - 1, 1, 1, Bounds.Height - 2);
            Add(seekBarTop);

            labelPosition = new HUDLabel("Loading...", "verdana22b2");
            labelPosition.Color = Lizk.SimpleHUD.ColorS.White;
            labelPosition.Alignment = Alignment.VCenter | Alignment.Right;
            labelPosition.Anchor = Anchor.Left | Anchor.Bottom;
            labelPosition.Bounds = new RectangleS(3, 1, 100, 24);
            labelPosition.CharacterOffset = new PointS(0, -2);
            Add(labelPosition);
            UpdateLabels();
        }

        void timer_TimeOut(object sender, EventArgs e)
        {
            if (SeekEnded != null)
                SeekEnded(this, EventArgs.Empty);

            timer.Reset(TimeSpan.Zero);
        }

        void timer_FirstTrigger(object sender, EventArgs e)
        {
            if (SeekBegun != null)
                SeekBegun(this, EventArgs.Empty);
        }
        public override void Update(TimeSpan ts)
        {
            timer.Update();
            base.Update(ts);
        }

    }
}
